# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from libcpp cimport bool as cbool

cdef extern from "melon/exception_wrapper.h" namespace "melon":
    cdef cppclass cMelonExceptionWrapper "melon::exception_wrapper":
        void throw_exception() except +
        T* get_exception[T]()


cdef extern from "melon/try.h" namespace "melon" nogil:
    cdef cppclass cMelonTry "melon::Try"[T]:
        T value()
        cbool hasException[T]()
        cbool hasException()
        cMelonExceptionWrapper exception()

cdef extern from "melon/futures/future.h" namespace "melon" nogil:
    cdef cppclass cMelonFuture "melon::Future"[T]:
        T get()
        cbool hasValue()
        cbool isReady()
        #TODO add via and then

    cdef cppclass cMelonSemiFuture "melon::SemiFuture"[T]:
        T get()
        pass

cdef extern from "melon/unit.h" namespace "melon":
    struct cMelonUnit "melon::Unit":
        pass

    cMelonUnit c_unit "melon::unit"

cdef extern from "melon/futures/promise.h" namespace "melon":
    cdef cppclass cMelonPromise "melon::Promise"[T]:
        cMelonPromise()
        cMelonPromise(cMelonPromise&&)
        void setValue[M](M& value)
        void setException[E](E& value)
        cMelonFuture[T] getFuture()

        @staticmethod
        cMelonPromise[T] makeEmpty()

cdef extern from "melon/executor.h" namespace "melon":
    cdef cppclass cMelonExecutor "melon::Executor":
        pass

cdef extern from "melon/portability.h" namespace "melon":
    cbool cMelonIsDebug "melon::kIsDebug"
    cbool cMelonIsTsan "melon::kIsSanitizeThread"
